package org.csu.mypetstore.persistence.impi;

import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.persistence.DBUtil;
import org.csu.mypetstore.persistence.UserCartDAO;
import org.csu.mypetstore.service.CatalogService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.zip.CheckedOutputStream;

public class UserCartDAOImpi implements UserCartDAO {
    private final static String GETUSERCART = "SELECT ITEMID,NUMBER FROM USERCART WHERE USERNAME = ?";
    private final static String UPDATECARTITEM = "UPDATE USERCART SET ITEMID = ?,NUMBER = ? WHERE USERNAME = ?";
    private final static String INSERT_NEW_ACCOUNT = "INSERT INTO USERCART (USERNAME,ITEMID,NUMBER) VALUES (?,?,?)";
//    INSERT INTO SIGNON (PASSWORD,USERNAME) VALUES (?, ?)
//    UPDATE PROFILE SET LANGPREF = ?, FAVCATEGORY = ? WHERE USERID = ?
//    SELECT name, nextid FROM SEQUENCE WHERE NAME = ?

    @Override
    public Cart getUserCartByUserName(String username) {
        CatalogService catelogService = new CatalogService();
        Cart cart = new Cart();
        try{
            String itemList = new String();
            String number = new String();
            String[] items;
            String[] nums;
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement(GETUSERCART);
            pStatement.setString(1,username);
            ResultSet resultSet = pStatement.executeQuery();
            resultSet.next();
            itemList = resultSet.getString(1);
            number = resultSet.getString(2);
            items = itemList.split(" ");
            nums = number.split(" ");


            if(items != null)
            {
                for(int i=0;i<items.length;i++)
                {
                    boolean isInStock = catelogService.isItemInStock(items[i]);
//                    System.out.println("查出来的 "+items[i]);
                    Item item = catelogService.getItem(items[i]);
                    cart.addItem(item,isInStock);
                    int tempInt = Integer.parseInt(nums[i]);
                    cart.setQuantityByItemId(items[i],tempInt);
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return cart;
    }

    @Override
    public void addCartItem(String username, String itemId) {
        try{
            String itemList = new String();
            String number = new String();
            Connection connection = DBUtil.getConnection();
//            PreparedStatement preparedStatement = connection.prepareStatement(getSequenceString);
            PreparedStatement pStatement = connection.prepareStatement(GETUSERCART);
            pStatement.setString(1,username);
            ResultSet resultSet = pStatement.executeQuery();
            if(resultSet.next()){
                itemList = resultSet.getString(1);
                number = resultSet.getString(2);
            }
            itemList = itemList+itemId+" ";
            number = number+1+" ";
            pStatement = connection.prepareStatement(UPDATECARTITEM);
            pStatement.setString(1,itemList);
            pStatement.setString(2,number);
            pStatement.setString(3,username);
            pStatement.executeUpdate();

            DBUtil.closePrepareStatement(pStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void updateUserCartByUserName(String username, String itemId, String newNumber) {
        try{
            String itemList = new String();
            String number = new String();
            String[] items;
            String[] nums;
            String resultItemList= new String();
            String resultNumber= new String();
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement(GETUSERCART);
            pStatement.setString(1,username);
            ResultSet resultSet = pStatement.executeQuery();
            resultSet.next();

            itemList = resultSet.getString(1);
            number = resultSet.getString(2);
            items = itemList.split(" ");
            nums = number.split(" ");


            for(int i=0;i<items.length;i++)
            {

                if(items[i].equals(itemId))
                {
                    resultItemList = resultItemList + items[i]+" ";
                    resultNumber = resultNumber + newNumber+" ";
                }
                else
                {
                    resultItemList = resultItemList + items[i]+" ";
                    resultNumber = resultNumber + nums[i]+" ";
                }
            }
            pStatement = connection.prepareStatement(UPDATECARTITEM);
            pStatement.setString(1,resultItemList);
            pStatement.setString(2,resultNumber);
            pStatement.setString(3,username);

            pStatement.executeUpdate();
            DBUtil.closePrepareStatement(pStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void removeItemById(String username,String itemId){
        try{
            String itemList = new String();
            String number = new String();
            String[] items;
            String[] nums;
            String resultItemList= new String();
            String resultNumber= new String();
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement(GETUSERCART);
            pStatement.setString(1,username);
            ResultSet resultSet = pStatement.executeQuery();
            resultSet.next();

            itemList = resultSet.getString(1);
            number = resultSet.getString(2);
            items = itemList.split(" ");
            nums = number.split(" ");


            for(int i=0;i<items.length;i++)
            {

                if(items[i].equals(itemId))
                {
                    System.out.println(items[i]+"移除");
                    continue;
                }
                else
                {
                    System.out.println(items[i]+"留");
                    resultItemList = resultItemList + items[i]+" ";
                    resultNumber = resultNumber + nums[i]+" ";
                }
            }
            pStatement = connection.prepareStatement(UPDATECARTITEM);
            pStatement.setString(1,resultItemList);
            pStatement.setString(2,resultNumber);
            pStatement.setString(3,username);

            pStatement.executeUpdate();
            DBUtil.closePrepareStatement(pStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  void insertAccount(String username){
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement(INSERT_NEW_ACCOUNT);
            pStatement.setString(1,username);
            pStatement.setString(2,"");
            pStatement.setString(3,"");

            pStatement.executeUpdate();
            DBUtil.closePrepareStatement(pStatement);
            DBUtil.closeConnection(connection);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void clearCart(String username){
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement pStatement = connection.prepareStatement(UPDATECARTITEM);
            pStatement.setString(1,"");
            pStatement.setString(2,"");
            pStatement.setString(3,username);
            pStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
